package com.ltmonitor.web.controller;

import java.lang.reflect.Proxy;
import java.net.URL;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


import javax.servlet.http.HttpServletRequest;
import javax.swing.plaf.synth.SynthToggleButtonUI;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.remoting.rmi.RmiProxyFactoryBean;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fasterxml.jackson.databind.jsonschema.JsonSchema;
import com.ltmonitor.entity.GnssData;
import com.ltmonitor.service.ITransferService;
import com.ltmonitor.service.ITransferServiceOld;
import com.ltmonitor.web.test.MyInvocationHandler;
import com.ltmonitor.web.util.FileUtil;
import com.ltmonitor.web.util.JsonMessage;
import com.ltmonitor.web.util.SpringApplicationContextHolder;
import com.sun.tools.jdi.EventSetImpl.Itr;

import net.sf.json.JSONObject;



@Controller
@RequestMapping("/testController")
public class TestController extends PaginateAction {
	private Logger mylog = Logger.getLogger("debug");
	
	//转发服务
	private ITransferService transferService;
//	transferService = SpringApplicationContextHolder.getBean("transferService");
	
//	@Value("${old_trans_url}")
//	private String  oldTransUrl;
	
	
	@RequestMapping("/viewTestPage.action")
	public String viewTestPage(Model model) {
		model.addAttribute("message", "hello");
		return "test/test";
	}
	
	/**
	 * 上传一个文件信息
	 * @param request
	 * @return
	 */
	@RequestMapping("/uploadOneFile.action")
	@ResponseBody
	public JsonMessage uploadOneFile(HttpServletRequest request ){
		JsonMessage result = null;
		String msg = "";
		try {
			Map map = new HashMap ();
			msg = FileUtil.checkParams(request, map);
			
			//开始保存文件
			if(msg.equals("")){
				msg = FileUtil.SaveOneFileData(request, map);
			}
			
			//格式化结果
			if(msg.equals("")){
				msg = "上传文件成功！";
				result = new JsonMessage(true, msg);
				result.setData(msg);
			}else {
				result = new JsonMessage(false, msg);
			}
			
		} catch (Exception e) {
			// TODO: handle exception
			msg = "上传文件错误：" + e.getMessage();
			e.printStackTrace();
			result = new JsonMessage(false, msg);
		}
		System.out.println(msg);
		System.out.println("操作完成....................");
		return result;
	}

	
	/**
	 * 请求测试数据
	 * @param jsonStr
	 * @return
	 */
	@RequestMapping("/testData.action")
	@ResponseBody
	public JsonMessage testData(@RequestBody String jsonStr ) {
		JsonMessage result = null;
		String msg = "";
		try {
			Map map = new HashMap ();
			msg = "message";

			result = new JsonMessage(false, msg);
		} catch (Exception e) {
			// TODO: handle exception
			msg  += "测试数据失败：" + e.getMessage();
			e.printStackTrace();
			result = new JsonMessage(false, msg);
		}
		
		return result;
	}


	
	@RequestMapping("/testTrans.action")
	@ResponseBody
	public JsonMessage testTrans(@RequestBody String jsonStr) {
		JsonMessage result = null;
		String msg = "";
		try {
			ITransferServiceOld transServiceOld = null;
			Map map = JSONObject.fromObject(jsonStr);
			if(map.containsKey("rmiUrl")) {
				
				String url = map.get("rmiUrl").toString();
				
				RmiProxyFactoryBean factory = new RmiProxyFactoryBean();
				factory.setServiceInterface(ITransferServiceOld.class);
				factory.setServiceUrl(url);
				
				factory.setLookupStubOnStartup(false);
				factory.setRefreshStubOnConnectFailure(true);
				factory.afterPropertiesSet();
				
				Object object = factory.getObject();
				MyInvocationHandler handler = new MyInvocationHandler(object);
				ClassLoader loader = InsertAndTransController.class.getClassLoader();
				Class<?>[] interfaces = object.getClass().getInterfaces();
				
				transServiceOld = (ITransferServiceOld) Proxy.newProxyInstance(loader, interfaces, handler);
				if(transServiceOld  == null) {
					msg += "对象为空！";
				}else {
					com.ltmonitor.entity.GnssData gpsData_old = new com.ltmonitor.entity.GnssData();
					transServiceOld.UpExgMsgRealLocation(gpsData_old);
					msg += "转发成功！";
				}
			}
			
			if(msg.equals("")) {
				result = new JsonMessage(true, transServiceOld.toString());
			}else {
				result = new JsonMessage(false, msg);
			}
		} catch (Exception e) {
			// TODO: handle exception
			msg += "获取转发对象失败：" + e.getMessage();
			result = new JsonMessage(false, msg);
			e.printStackTrace();
		}
		
		
		return result;
	}
	
}
